package com.dnw.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dnw.base.MyApp;
import com.dnw.modle.Program;
import com.dnw.shyfunny.ProgramDetailActivity;
import com.dnw.shyfunny.R;
import com.dnw.util.GlideUtils;
import com.dnw.util.RecommendProgramUtil;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author ChayChan
 * @description: 频道的adapter
 * @date 2017/6/16  9:45
 */

public class ProgramAdapter extends BaseAdapter {
    private Context       mContext;
    private List<Program> mPrograms;

    public ProgramAdapter(Context context, List<Program> programs) {
        mContext = context;
        mPrograms = programs;
    }

    @Override
    public int getCount() {
        return mPrograms.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(null == mContext){
            mContext = MyApp.getContext();
        }
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_program, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
        final Program program = mPrograms.get(position);
        holder.tvName.setText(program.name);
        holder.tvInfo.setText(program.score + "分  " + program.downloadCount + "人下载     " + program.size);
        holder.tvSlogan.setText(program.slogan);
        GlideUtils.load(mContext, program.iconDownloadUrl, holder.ivIcon, R.color.divider);//设置缩略图
        switch (RecommendProgramUtil.checkProgramStatus(mContext, program.name)) {
            case RecommendProgramUtil.UNDOWNLOAD:
                holder.btDownload.setText("下载");
                holder.btDownload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext, ProgramDetailActivity.class);
                        intent.putExtra("program", program);
                        intent.putExtra("download", true);
                        mContext.startActivity(intent);
                    }
                });
                break;
            case RecommendProgramUtil.DOWNLOAD:
                holder.btDownload.setText("安装");
                holder.btDownload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        File file= new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "xiudou_app" + File.separator + program.name + ".apk");
                        //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
                        Uri apkUri =
                                FileProvider.getUriForFile(mContext, "com.dnw.shyfunny.fileprovider", file);

                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        // 由于没有在Activity环境下启动Activity,设置下面的标签
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        //添加这一句表示对目标应用临时授权该Uri所代表的文件
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
                        mContext.startActivity(intent);
                    }
                });

                break;
            case RecommendProgramUtil.INSTALLED:
                holder.btDownload.setText("打开");
                holder.btDownload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        RecommendProgramUtil.startApplicationWithName(mContext, program.name);
                    }
                });
                break;
        }
        holder.rlProgram.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ProgramDetailActivity.class);
                intent.putExtra("program", program);
                mContext.startActivity(intent);
            }
        });


        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tv_name)
        TextView       tvName;
        @Bind(R.id.tv_info)
        TextView       tvInfo;
        @Bind(R.id.tv_slogan)
        TextView       tvSlogan;
        @Bind(R.id.iv_icon)
        ImageView      ivIcon;
        @Bind(R.id.bt_download)
        Button         btDownload;
        @Bind(R.id.rl_program)
        RelativeLayout rlProgram;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
